local Const = require('const')
local Camera = require("shake_camera")
local Conf = Const(require("config"))
local Tween = require('timer')
local scale = 0.5
local cameraX = 0
local cameraY = 0

function love.load()

	love.physics.setMeter(64) --the height of a meter our worlds will be 64px
	world = love.physics.newWorld(0, 9.81*64, true) --create a world for the bodies to exist in with horizontal gravity of 0 and vertical gravity of 9.81

	objects = {} -- table to hold all our physical objects

	--let's create the ground
	objects.ground = {}
	objects.ground.body = love.physics.newBody(world, Conf.winWidth / 2, Conf.winHeight) --remember, the shape (the rectangle we create next) anchors to the body from its center, so we have to move it to (650/2, 650-50/2)
	objects.ground.shape = love.physics.newRectangleShape(Conf.winWidth, 10) --make a rectangle with a width of 650 and a height of 50
	objects.ground.fixture = love.physics.newFixture(objects.ground.body, objects.ground.shape); --attach shape to body

	objects.ground_left = {}
	objects.ground_left.body = love.physics.newBody(world, 0, Conf.winHeight / 2)
	objects.ground_left.shape = love.physics.newRectangleShape(10, Conf.winHeight)
	objects.ground_left.fixture = love.physics.newFixture(objects.ground_left.body, objects.ground_left.shape)

	objects.ground_right = {}
	objects.ground_right.body = love.physics.newBody(world, Conf.winWidth, Conf.winHeight / 2)
	objects.ground_right.shape = love.physics.newRectangleShape(10, Conf.winHeight)
	objects.ground_right.fixture = love.physics.newFixture(objects.ground_right.body, objects.ground_right.shape)

	objects.ground_top = {}
	objects.ground_top.body = love.physics.newBody(world, Conf.winWidth / 2, 0)
	objects.ground_top.shape = love.physics.newRectangleShape(Conf.winWidth, 10)
	objects.ground_top.fixture = love.physics.newFixture(objects.ground_top.body, objects.ground_top.shape)

	--let's create a ball
	objects.ball = {}
	local ballX, ballY = 150, 150
	objects.ball.body = love.physics.newBody(world, ballX, ballY, "dynamic") --place the body in the center of the world and make it dynamic, so it can move around
	objects.ball.shape = love.physics.newCircleShape(50) --the ball's shape has a radius of 20
	objects.ball.fixture = love.physics.newFixture(objects.ball.body, objects.ball.shape, 1) -- Attach fixture to body and give it a density of 1.
	objects.ball.fixture:setRestitution(.8) --let the ball bounce
	objects.ball.body:setGravityScale(-1)

	--let's create a couple blocks to play around with
	objects.block1 = {}
	objects.block1.body = love.physics.newBody(world, 50, 50, "dynamic")
	objects.block1.body:setGravityScale(1)
	objects.block1.shape = love.physics.newRectangleShape(40, 40)
	objects.block1.fixture = love.physics.newFixture(objects.block1.body, objects.block1.shape, 5) -- A higher density gives it more mass.

	-- connect ball with block1
	x1, y1 = objects.block1.body:getWorldCenter()
	x2, y2 = objects.ball.body:getWorldCenter()
	distanceJoint = love.physics.newDistanceJoint(
			objects.block1.body,
			objects.ball.body,
			x1, y1,
			x2, y2)

	--initial graphics setup
	love.graphics.setBackgroundColor(104, 136, 248) --set the background color to a nice blue
	
	cam = Camera:new()
	cam:shake(2, 60, 2)	

	back_color = {0, 0, 0}
	Tween.tween(10, back_color, {255, 255, 255}, 'in-out-quad')
end

local range = 30
local delta = .2
local dir = 1

function love.update(dt)
	world:update(dt) --this puts the world into motion

	Tween.update(dt)
	--here we are going to create some keyboard events
	if love.keyboard.isDown("right") then --press the right arrow key to push the ball to the right
		objects.ball.body:applyForce(100, 0)
	end
	if love.keyboard.isDown("left") then --press the left arrow key to push the ball to the left
		objects.ball.body:applyForce(-100, 0)
	end
	if love.keyboard.isDown("up") then --press the up arrow key to set the ball in the air
		objects.ball.body:applyForce(0, -400)
	end
	if love.keyboard.isDown('down') then
		objects.ball.body:applyForce(0, 50)
	end
	if love.keyboard.isDown('n') then
		cam:restart()
	end
	if love.keyboard.isDown(" ") then
		objects.ball.body:setPosition(650/2, 650/2)
	end
	if love.keyboard.isDown('u') then
		scale = scale + .1
	end
	if love.keyboard.isDown('j') then
		scale = scale - .1
	end
	local cameraSpeed = 10
	if love.keyboard.isDown("a") then
		cameraX = cameraX + cameraSpeed	
	elseif love.keyboard.isDown("d") then
		cameraX = cameraX - cameraSpeed
	end
	if love.keyboard.isDown("w") then
		cameraY = cameraY + cameraSpeed
	elseif love.keyboard.isDown("s") then
		cameraY = cameraY - cameraSpeed
	end

	cam:lookAt(objects.ball.body:getPosition())
	cam:update(dt)
end

function love.draw()
	--love.graphics.translate(cameraX, cameraY)
	--love.graphics.scale(scale, scale)


	love.graphics.setBackgroundColor(back_color[1], back_color[2], back_color[3])

	cam:beginScene()
	love.graphics.setColor(72, 160, 14) -- set the drawing color to green for the ground
	-- draw the stick
	x1, y1 = objects.ball.body:getWorldCenter()
	x2, y2 = objects.block1.body:getWorldCenter()
	love.graphics.line(x1, y1, x2, y2)

	love.graphics.polygon("fill", objects.ground.body:getWorldPoints(objects.ground.shape:getPoints())) 
	love.graphics.polygon("fill", objects.ground_left.body:getWorldPoints(objects.ground_left.shape:getPoints())) 
	love.graphics.polygon("fill", objects.ground_right.body:getWorldPoints(objects.ground_right.shape:getPoints())) 
	love.graphics.polygon("fill", objects.ground_top.body:getWorldPoints(objects.ground_top.shape:getPoints())) 

	love.graphics.setColor(193, 47, 14) --set the drawing color to red for the ball
	love.graphics.circle("fill", objects.ball.body:getX(), objects.ball.body:getY(), objects.ball.shape:getRadius())

	love.graphics.setColor(50, 50, 50) -- set the drawing color to grey for the blocks
	love.graphics.polygon("fill", objects.block1.body:getWorldPoints(objects.block1.shape:getPoints()))

	cam:endScene()
end

